package com.keon.jianbao.ui.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.view.View;

import com.keon.jianbao.AppContext;
import com.keon.jianbao.AppManager;
import com.keon.jianbao.R;
import com.keon.jianbao.api.Api;
import com.keon.jianbao.api.SimpleHandler;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.bean.AppUpdate;
import com.keon.jianbao.bean.Result;
import com.keon.jianbao.ui.widget.ItemLayout;
import com.keon.jianbao.util.AppFileDownUtils;
import com.keon.jianbao.util.DialogHelp;
import com.keon.jianbao.util.FileUtils;
import com.keon.jianbao.util.JsonUtils;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONException;

import java.io.File;

import butterknife.Bind;

/**
 * Created by keon on 2015/9/8.
 */
public class MoreActivity extends BaseActivity {

    String SDPATH = "/sdcard/jianbao/thumbnail/";
    String tel = "4008038211";

    @Bind(R.id.more_logout)
    ItemLayout ilLogout;
    @Bind(R.id.more_feedback)
    ItemLayout ilFeedBack;
    @Bind(R.id.more_cache)
    ItemLayout ilCache;
    @Bind(R.id.more_tel)
    ItemLayout ilTel;
    @Bind(R.id.more_about)
    ItemLayout ilAbout;
    @Bind(R.id.more_term)
    ItemLayout ilTerm;
    @Bind(R.id.more_update)
    ItemLayout ilUpdate;

    Handler handler = new Handler() {

    };

    AsyncHttpResponseHandler logoutHandler = new SimpleHandler(this) {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            super.onSuccess(statusCode, headers, responseBody);
            Result result = null;
            try {
                result = new Result(responseBody);
                if (result.getStatus() > 0) {
                    MyInfoActivity.showToast(result.getTips());

                    AppContext.getInstance().Logout();

                    AppManager.getAppManager().finishActivity();
                    jump(MainActivity.class);
                } else {
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    SimpleHandler updateHandler = new SimpleHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            super.onSuccess(statusCode, headers, responseBody);
            try {
                final Result result = new Result(responseBody);
                if (result.getStatus() > 0) {
                    final AppUpdate appUpdate = JsonUtils.fromJson(result.getContent(), AppUpdate.class);
                    int version = AppContext.getInstance().getVersion();
                    if (appUpdate.getVersionCode() > version) {
                        if (appUpdate.isGotta()) {
                            DialogHelp.getConfirmDialog2(MoreActivity.this, "点击确认后开始升级<br/> <br/> 大小：" + appUpdate.getSize()
                                    + "<br/>描述：" + appUpdate.getDescription(), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    AppFileDownUtils appFileDownUtils =
                                            new AppFileDownUtils(MoreActivity.this, handler,
                                                    "http://bcs.91.com/pcsuite-dev/channel/8456d096da23decc9261c10ad28acb92.apk", "ll.apk");
                                    appFileDownUtils.start();

                                }
                            }).show();
                        } else {
                            DialogHelp.getConfirmDialog(MoreActivity.this, "是否升级<br/> <br/> 大小：" + appUpdate.getSize()
                                    + "<br/>描述：" + appUpdate.getDescription(), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
//                                    FileUtils.update(MoreActivity.this, appUpdate.getDownload());
                                    AppFileDownUtils appFileDownUtils =
                                            new AppFileDownUtils(MoreActivity.this, handler,
                                                    "http://bcs.91.com/pcsuite-dev/channel/8456d096da23decc9261c10ad28acb92.apk", "ll.apk");
                                    appFileDownUtils.start();
                                }
                            }).show();
                        }
                    }


                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_more;
    }

    @Override
    protected int getToolBarTitle() {
        return R.string.more;
    }

    @Override
    public void initView() {
        ilLogout.setOnClickListener(this);
        ilFeedBack.setOnClickListener(this);
        ilCache.setOnClickListener(this);
        ilTel.setOnClickListener(this);
        ilAbout.setOnClickListener(this);
        ilTerm.setOnClickListener(this);
        ilUpdate.setOnClickListener(this);
    }

    @Override
    public void initData() {
        updateHandler.setContext(this);

        initCache();

        ilTel.setSummary(tel);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.more_logout:
                showWaitDialog();
                Api.logout(AppContext.getInstance().getProperty("user.code"), logoutHandler);
                break;
            case R.id.more_feedback:
                jump(FeedBackActivity.class);
                break;
            case R.id.more_cache:
                showWaitDialog();
                FileUtils.clearFileWithPath(SDPATH);
                hideWaitDialog();
                showToast("操作成功");
                initCache();
                break;
            case R.id.more_tel:
                tel(tel);
                break;
            case R.id.more_about:
                jump(AboutActivity.class);
                break;
            case R.id.more_term:
                jump(TermActivity.class);
                break;
            case R.id.more_update:
                showWaitDialog();
                Api.apk_update(updateHandler);
                break;
        }
    }

    void initCache() {

        File file = new File(SDPATH);
        String size = FileUtils.formatFileSize(FileUtils.getDirSize(file));
        ilCache.setText(size);
    }

    void tel(final String number) {
        DialogHelp.getConfirmDialog(this, "拨打客服电话", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
                startActivity(intent);
            }
        }).show();

    }
}
